import { defineComponent, ref, onMounted, reactive } from 'vue';
import { NButton, NUpload, NInput, useMessage, NPopover } from 'naive-ui';
import axios from 'axios';
export default defineComponent({
    name: 'FilesView',
    components: {
        NButton,
        NUpload,
        NInput,
        NPopover
    },
    setup() {
        const files = ref([]);
        const folderName = ref('');
        const message = useMessage();
        const currentPath = ref('');
        const renameMap = reactive({});
        const fetchFiles = async () => {
            try {
                const response = await axios.post('/api/files', { path: currentPath.value });
                console.log('Fetched files:', response.data); // 输出数据，检查是否正常，删除
                if (Array.isArray(response.data)) {
                    files.value = response.data.map(file => {
                        const formattedDate = new Date(file.modTime * 1000).toLocaleString();
                        const formattedNumber = new Number((file.size / Math.pow(1024, 2)).toFixed(1));
                        return {
                            ...file,
                            formattedModTime: formattedDate,
                            formattedSize: formattedNumber
                        };
                    }); // 确保 response.data 是数组
                }
                else {
                    files.value = []; // 如果数据格式不对，设置为空数组，避免错误
                }
            }
            catch (error) {
                console.error('Error fetching files:', error); // 打印错误信息
                message.error('文件加载失败');
            }
        };
        const enterFolder = (folderName) => {
            currentPath.value = currentPath.value ? `${currentPath.value}/${folderName}` : folderName;
            fetchFiles();
        };
        const goBack = () => {
            if (!currentPath.value)
                return;
            const parts = currentPath.value.split('/');
            parts.pop();
            currentPath.value = parts.join('/');
            fetchFiles();
        };
        // 在组件加载时自动执行 fetchFiles
        onMounted(() => {
            fetchFiles();
            console.log('正在获取文件...');
        });
        const handleUploadFinish = () => {
            fetchFiles();
            message.success('加载成功');
        };
        const downloadFile = async (name) => {
            try {
                const response = await axios.get(`/api/download?name=${name}`, { responseType: 'blob' });
                const url = window.URL.createObjectURL(new Blob([response.data]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', name);
                document.body.appendChild(link);
                link.click();
                message.success('下载成功');
            }
            catch (error) {
                message.error('下载...');
            }
        };
        const deleteFile = async (name) => {
            try {
                await axios.post('/api/delete', { name });
                fetchFiles();
                message.success('删除成功');
            }
            catch (error) {
                message.error('删除失败');
            }
        };
        const renameFile = async (oldName) => {
            const newName = renameMap[oldName];
            if (!newName || newName.trim() === '') {
                message.warning('新名称不能为空');
                return;
            }
            try {
                await axios.post('/api/rename', { oldName, newName: newName.trim() });
                fetchFiles();
                renameMap[oldName] = '';
                message.success('重命名成功');
            }
            catch (error) {
                message.error('重命名失败');
            }
        };
        const addFolder = async () => {
            const name = folderName.value;
            const path = currentPath.value;
            if (!name || name.trim() === '') {
                message.warning('文件夹名不能为空');
                return;
            }
            try {
                await axios.post('/api/addFolder', { name: name.trim(), path: path });
                fetchFiles();
                folderName.value = '';
                message.success('文件夹添加成功');
            }
            catch (erro) {
                console.log(erro);
                message.error('文件夹添加失败');
            }
        };
        return {
            files,
            folderName,
            currentPath,
            renameMap,
            enterFolder,
            goBack,
            fetchFiles,
            handleUploadFinish,
            downloadFile,
            deleteFile,
            renameFile,
            addFolder
        };
    }
});
debugger; /* PartiallyEnd: #3632/script.vue */
const __VLS_ctx = {};
const __VLS_componentsOption = {
    NButton,
    NUpload,
    NInput,
    NPopover
};
let __VLS_components;
let __VLS_directives;
// CSS variable injection 
// CSS variable injection end 
__VLS_asFunctionalElement(__VLS_intrinsicElements.div, __VLS_intrinsicElements.div)({});
__VLS_asFunctionalElement(__VLS_intrinsicElements.h1, __VLS_intrinsicElements.h1)({});
__VLS_asFunctionalElement(__VLS_intrinsicElements.p, __VLS_intrinsicElements.p)({});
(__VLS_ctx.currentPath || '/');
if (__VLS_ctx.currentPath) {
    const __VLS_0 = {}.NButton;
    /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
    // @ts-ignore
    const __VLS_1 = __VLS_asFunctionalComponent(__VLS_0, new __VLS_0({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }));
    const __VLS_2 = __VLS_1({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }, ...__VLS_functionalComponentArgsRest(__VLS_1));
    let __VLS_4;
    let __VLS_5;
    let __VLS_6;
    const __VLS_7 = {
        onClick: (__VLS_ctx.goBack)
    };
    __VLS_3.slots.default;
    var __VLS_3;
}
const __VLS_8 = {}.NButton;
/** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
// @ts-ignore
const __VLS_9 = __VLS_asFunctionalComponent(__VLS_8, new __VLS_8({
    ...{ 'onClick': {} },
    ...{ style: {} },
}));
const __VLS_10 = __VLS_9({
    ...{ 'onClick': {} },
    ...{ style: {} },
}, ...__VLS_functionalComponentArgsRest(__VLS_9));
let __VLS_12;
let __VLS_13;
let __VLS_14;
const __VLS_15 = {
    onClick: (__VLS_ctx.fetchFiles)
};
__VLS_11.slots.default;
var __VLS_11;
const __VLS_16 = {}.NPopover;
/** @type {[typeof __VLS_components.NPopover, typeof __VLS_components.nPopover, typeof __VLS_components.NPopover, typeof __VLS_components.nPopover, ]} */ ;
// @ts-ignore
const __VLS_17 = __VLS_asFunctionalComponent(__VLS_16, new __VLS_16({
    trigger: "click",
    placement: "bottom-start",
}));
const __VLS_18 = __VLS_17({
    trigger: "click",
    placement: "bottom-start",
}, ...__VLS_functionalComponentArgsRest(__VLS_17));
__VLS_19.slots.default;
{
    const { trigger: __VLS_thisSlot } = __VLS_19.slots;
    const __VLS_20 = {}.NButton;
    /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
    // @ts-ignore
    const __VLS_21 = __VLS_asFunctionalComponent(__VLS_20, new __VLS_20({
        ...{ style: {} },
    }));
    const __VLS_22 = __VLS_21({
        ...{ style: {} },
    }, ...__VLS_functionalComponentArgsRest(__VLS_21));
    __VLS_23.slots.default;
    var __VLS_23;
}
__VLS_asFunctionalElement(__VLS_intrinsicElements.div, __VLS_intrinsicElements.div)({});
const __VLS_24 = {}.NInput;
/** @type {[typeof __VLS_components.NInput, typeof __VLS_components.nInput, ]} */ ;
// @ts-ignore
const __VLS_25 = __VLS_asFunctionalComponent(__VLS_24, new __VLS_24({
    ...{ 'onUpdate:value': {} },
    value: (__VLS_ctx.folderName),
    placeholder: "请输入文件夹名：",
}));
const __VLS_26 = __VLS_25({
    ...{ 'onUpdate:value': {} },
    value: (__VLS_ctx.folderName),
    placeholder: "请输入文件夹名：",
}, ...__VLS_functionalComponentArgsRest(__VLS_25));
let __VLS_28;
let __VLS_29;
let __VLS_30;
const __VLS_31 = {
    'onUpdate:value': ((val) => __VLS_ctx.folderName = val)
};
var __VLS_27;
const __VLS_32 = {}.NButton;
/** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
// @ts-ignore
const __VLS_33 = __VLS_asFunctionalComponent(__VLS_32, new __VLS_32({
    ...{ 'onClick': {} },
}));
const __VLS_34 = __VLS_33({
    ...{ 'onClick': {} },
}, ...__VLS_functionalComponentArgsRest(__VLS_33));
let __VLS_36;
let __VLS_37;
let __VLS_38;
const __VLS_39 = {
    onClick: (...[$event]) => {
        __VLS_ctx.addFolder();
    }
};
__VLS_35.slots.default;
var __VLS_35;
var __VLS_19;
const __VLS_40 = {}.NUpload;
/** @type {[typeof __VLS_components.NUpload, typeof __VLS_components.nUpload, typeof __VLS_components.NUpload, typeof __VLS_components.nUpload, ]} */ ;
// @ts-ignore
const __VLS_41 = __VLS_asFunctionalComponent(__VLS_40, new __VLS_40({
    ...{ 'onFinish': {} },
    action: "/api/upload",
    headers: ({}),
    data: ({}),
}));
const __VLS_42 = __VLS_41({
    ...{ 'onFinish': {} },
    action: "/api/upload",
    headers: ({}),
    data: ({}),
}, ...__VLS_functionalComponentArgsRest(__VLS_41));
let __VLS_44;
let __VLS_45;
let __VLS_46;
const __VLS_47 = {
    onFinish: (__VLS_ctx.handleUploadFinish)
};
__VLS_43.slots.default;
const __VLS_48 = {}.NButton;
/** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
// @ts-ignore
const __VLS_49 = __VLS_asFunctionalComponent(__VLS_48, new __VLS_48({
    ...{ style: {} },
}));
const __VLS_50 = __VLS_49({
    ...{ style: {} },
}, ...__VLS_functionalComponentArgsRest(__VLS_49));
__VLS_51.slots.default;
var __VLS_51;
var __VLS_43;
__VLS_asFunctionalElement(__VLS_intrinsicElements.ul, __VLS_intrinsicElements.ul)({});
for (const [file] of __VLS_getVForSourceType((__VLS_ctx.files))) {
    __VLS_asFunctionalElement(__VLS_intrinsicElements.li, __VLS_intrinsicElements.li)({
        key: (file.name),
    });
    if (file.isDir) {
        __VLS_asFunctionalElement(__VLS_intrinsicElements.span, __VLS_intrinsicElements.span)({
            ...{ onClick: (...[$event]) => {
                    if (!(file.isDir))
                        return;
                    __VLS_ctx.enterFolder(file.name);
                } },
            ...{ style: {} },
        });
        (file.name);
        (file.formattedModTime);
        const __VLS_52 = {}.NButton;
        /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
        // @ts-ignore
        const __VLS_53 = __VLS_asFunctionalComponent(__VLS_52, new __VLS_52({
            ...{ 'onClick': {} },
            ...{ style: {} },
        }));
        const __VLS_54 = __VLS_53({
            ...{ 'onClick': {} },
            ...{ style: {} },
        }, ...__VLS_functionalComponentArgsRest(__VLS_53));
        let __VLS_56;
        let __VLS_57;
        let __VLS_58;
        const __VLS_59 = {
            onClick: (...[$event]) => {
                if (!(file.isDir))
                    return;
                __VLS_ctx.downloadFile(file.name);
            }
        };
        __VLS_55.slots.default;
        var __VLS_55;
    }
    else {
        (file.name);
        (file.formattedSize);
        (file.formattedModTime);
        const __VLS_60 = {}.NButton;
        /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
        // @ts-ignore
        const __VLS_61 = __VLS_asFunctionalComponent(__VLS_60, new __VLS_60({
            ...{ 'onClick': {} },
            ...{ style: {} },
        }));
        const __VLS_62 = __VLS_61({
            ...{ 'onClick': {} },
            ...{ style: {} },
        }, ...__VLS_functionalComponentArgsRest(__VLS_61));
        let __VLS_64;
        let __VLS_65;
        let __VLS_66;
        const __VLS_67 = {
            onClick: (...[$event]) => {
                if (!!(file.isDir))
                    return;
                __VLS_ctx.downloadFile(file.name);
            }
        };
        __VLS_63.slots.default;
        var __VLS_63;
    }
    const __VLS_68 = {}.NButton;
    /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
    // @ts-ignore
    const __VLS_69 = __VLS_asFunctionalComponent(__VLS_68, new __VLS_68({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }));
    const __VLS_70 = __VLS_69({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }, ...__VLS_functionalComponentArgsRest(__VLS_69));
    let __VLS_72;
    let __VLS_73;
    let __VLS_74;
    const __VLS_75 = {
        onClick: (...[$event]) => {
            __VLS_ctx.deleteFile(file.name);
        }
    };
    __VLS_71.slots.default;
    var __VLS_71;
    const __VLS_76 = {}.NPopover;
    /** @type {[typeof __VLS_components.NPopover, typeof __VLS_components.nPopover, typeof __VLS_components.NPopover, typeof __VLS_components.nPopover, ]} */ ;
    // @ts-ignore
    const __VLS_77 = __VLS_asFunctionalComponent(__VLS_76, new __VLS_76({
        trigger: "click",
        placement: "bottom-start",
    }));
    const __VLS_78 = __VLS_77({
        trigger: "click",
        placement: "bottom-start",
    }, ...__VLS_functionalComponentArgsRest(__VLS_77));
    __VLS_79.slots.default;
    {
        const { trigger: __VLS_thisSlot } = __VLS_79.slots;
        const __VLS_80 = {}.NButton;
        /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
        // @ts-ignore
        const __VLS_81 = __VLS_asFunctionalComponent(__VLS_80, new __VLS_80({
            ...{ style: {} },
        }));
        const __VLS_82 = __VLS_81({
            ...{ style: {} },
        }, ...__VLS_functionalComponentArgsRest(__VLS_81));
        __VLS_83.slots.default;
        var __VLS_83;
    }
    __VLS_asFunctionalElement(__VLS_intrinsicElements.div, __VLS_intrinsicElements.div)({});
    const __VLS_84 = {}.NInput;
    /** @type {[typeof __VLS_components.NInput, typeof __VLS_components.nInput, ]} */ ;
    // @ts-ignore
    const __VLS_85 = __VLS_asFunctionalComponent(__VLS_84, new __VLS_84({
        ...{ 'onUpdate:value': {} },
        value: (__VLS_ctx.renameMap[file.name]),
        placeholder: "请输入新名称：",
    }));
    const __VLS_86 = __VLS_85({
        ...{ 'onUpdate:value': {} },
        value: (__VLS_ctx.renameMap[file.name]),
        placeholder: "请输入新名称：",
    }, ...__VLS_functionalComponentArgsRest(__VLS_85));
    let __VLS_88;
    let __VLS_89;
    let __VLS_90;
    const __VLS_91 = {
        'onUpdate:value': ((val) => __VLS_ctx.renameMap[file.name] = val)
    };
    var __VLS_87;
    const __VLS_92 = {}.NButton;
    /** @type {[typeof __VLS_components.NButton, typeof __VLS_components.nButton, typeof __VLS_components.NButton, typeof __VLS_components.nButton, ]} */ ;
    // @ts-ignore
    const __VLS_93 = __VLS_asFunctionalComponent(__VLS_92, new __VLS_92({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }));
    const __VLS_94 = __VLS_93({
        ...{ 'onClick': {} },
        ...{ style: {} },
    }, ...__VLS_functionalComponentArgsRest(__VLS_93));
    let __VLS_96;
    let __VLS_97;
    let __VLS_98;
    const __VLS_99 = {
        onClick: (...[$event]) => {
            __VLS_ctx.renameFile(file.name);
        }
    };
    __VLS_95.slots.default;
    var __VLS_95;
    var __VLS_79;
}
var __VLS_dollars;
let __VLS_self;
